/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * SCMS Class:
 * This is the main runtime class, which contains the main method, as well as object creation
 * @author Vale
 *
 */
public class SCMS {

    /**
     * @param args the command line arguments
     */
    private ProductList prodList = new ProductList();
    private InventoryList invList = new InventoryList();
    private final String INVALID_OPTION = "You did not enter a valid option.";
    private final String ERR = "Error: ";
    private final String CHOOSE_OPTION = "Please Choose an Option";
    private final String OPT = "Option: ";
    private final String MAIN_OPT0 = "0. Exit";
    private final String MAIN_OPT1 = "1. View/Edit Products";
    private final String MAIN_OPT2 = "2. View/Edit Current Inventory";
    private final String MAIN_OPT3 = "3. Schedule Products for Manufacturing and View Required Inventory";
    private final String MAIN_OPT4 = "4. Read Current Inventory from File";
    private final String MAIN_OPT5 = "5. Save Current Inventory to File";
    private final int OPT0 = 0;
    private final int OPT1 = 1;
    private final int OPT2 = 2;
    private final int OPT3 = 3;
    private final int OPT4 = 4;
    private final int OPT5 = 5;
    private final int FALSE = -1;

    /**
     * static void main(String[] args) Method:
     * Main Method - Starts Program
     *
     */
    public static void main(String[] args) {
        // TODO code application logic here
        SCMS newSCMS = new SCMS();
        newSCMS.startSCMS();

    }

    /**
     * static void startSCMS() Method:
     * Initialises all Objects in Product and Inventory Array, before printing main menu
     *
     */
    public void startSCMS() {
        Cheeseburger cheeseburger = new Cheeseburger();
        cheeseburger.setProdName("Cheeseburger");
        cheeseburger.setProdCost(2.35);
        cheeseburger.makeBurger();
        prodList.add(cheeseburger);
        BigMac bigMac = new BigMac();
        bigMac.setProdName("Big Mac");
        bigMac.setProdCost(4.25);
        bigMac.makeBurger();
        prodList.add(bigMac);
        McChicken mcChicken = new McChicken();
        mcChicken.setProdName("McChicken Burger");
        mcChicken.setProdCost(4.75);
        mcChicken.makeBurger();
        prodList.add(mcChicken);
        QuarterPounder qPounder = new QuarterPounder();
        qPounder.setProdName("Quarter Pounder");
        qPounder.setProdCost(4.75);
        qPounder.makeBurger();
        prodList.add(qPounder);
        McOz mcOz = new McOz();
        mcOz.setProdName("McOz Burger");
        mcOz.setProdCost(4.75);
        mcOz.makeBurger();
        prodList.add(mcOz);
        CrispyChicken crispyChicken = new CrispyChicken();
        crispyChicken.setProdName("Crispy Chicken Burger");
        crispyChicken.setProdCost(5.25);
        crispyChicken.makeBurger();
        prodList.add(crispyChicken);
        Combo mcOzCombo = new Combo();
        mcOzCombo.makeCombo(mcOz, 2, "Coke");
        //McOz tempCombo = (McOz) mcOzCombo.getMainProd();
        //tempCombo.makeBurger();
        mcOzCombo.setInventList(mcOzCombo.getMainProd().getInventList());
        prodList.add(mcOzCombo);
        Lettuce lettuce = new Lettuce();
        invList.add(lettuce);
        BeefPatty beefPatty = new BeefPatty();
        invList.add(beefPatty);
        Beetroot beetroot = new Beetroot();
        invList.add(beetroot);
        Tomato tomato = new Tomato();
        invList.add(tomato);
        Onions onions = new Onions();
        invList.add(onions);
        Pickles pickles = new Pickles();
        invList.add(pickles);
        Buns buns = new Buns();
        invList.add(buns);
        ChickenPatty chickenPatty = new ChickenPatty();
        invList.add(chickenPatty);
        Sauce sauce = new Sauce();
        invList.add(sauce);
        Cheese cheese = new Cheese();
        invList.add(cheese);


        printMain();
    }

    /**
     * void printMain() Method:
     * Prints Main Menu, and calls various functions depending on the user input
     *
     */
    public void printMain() {
        String choice = null;
        System.out.println(CHOOSE_OPTION);
        System.out.println('\t' + MAIN_OPT1);
        System.out.println('\t' + MAIN_OPT2);
        System.out.println('\t' + MAIN_OPT3);
        System.out.println('\t' + MAIN_OPT4);
        System.out.println('\t' + MAIN_OPT5);
        System.out.println('\t' + MAIN_OPT0);
        System.out.print(OPT);
        try {
            BufferedReader choiceIn = new BufferedReader(new InputStreamReader(System.in));
            choice = choiceIn.readLine();
            switch (checkInput(choice)) {
                case OPT0:
                    return;
                case OPT1:
                    prodList.printProducts();
                    printMain();
                    break;
                case OPT2:
                    invList.printInventory();
                    printMain();
                    break;
                case OPT3:
                    prodList.scheduleProducts(invList, prodList);
                    printMain();
                    break;
                case OPT4:
                    ExternalFile rFile = new ExternalFile();
                    rFile.readFile(invList);
                    printMain();
                    break;
                case OPT5:
                    ExternalFile wFile = new ExternalFile();
                    wFile.writeFile(invList);
                    printMain();
                    break;
                default:
                    System.out.println(INVALID_OPTION);
                    printMain();
            }
        } catch (IOException x) {
            System.out.println(ERR + x);
        }
    }

    /**
     * int checkInput(String choice) Method:
     * Takes the input choice from the main menu and checks to make sure it is correct.
     * @param choice This is the input String as entered by the user
     * @return Null String, invalid numbers or numbers out of bounds of array return FALSE, choiceIn otherwise
     *
     */
    public int checkInput(String choice) {
        if (choice.equals("")) {
            return FALSE;
        } else {
            try {
                int choiceIn = Integer.parseInt(choice);
                if (choiceIn < OPT0 || choiceIn > OPT5) {
                    return FALSE;
                } else {
                    return choiceIn;
                }
            } catch (NumberFormatException x) {
                return FALSE;
            }
        }
    }
}
